package STO_32_2;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import LCommon.TreeNode;

public class Solution {
	public List<List<Integer>> levelOrder(TreeNode root) {
		/*
		 * List<List<Integer>> res = new ArrayList<List<Integer>>(); Queue<TreeNode>
		 * queue = new LinkedList<TreeNode>(); if (root != null) { queue.add(root); }
		 * while (!queue.isEmpty()) { List<Integer> temp = new ArrayList<Integer>(); for
		 * (int i = 0; i < queue.size(); i++) { TreeNode node = queue.poll();
		 * temp.add(node.val); if (node.left != null) { queue.add(node.left); } if
		 * (node.right != null) { queue.add(node.right); } } res.add(temp); }
		 * 
		 * return res;
		 */
		
		Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i > 0; i--) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            res.add(tmp);
        }
        return res;

		
	}
}
